import sqlite3


def init_db():
    conn = sqlite3.connect('database.db')
    cursor = conn.cursor()

    cursor.execute('''
        CREATE TABLE IF NOT EXISTS access_auth (
            id INTEGER PRIMARY KEY,
            key VARCHAR(32) NOT NULL UNIQUE,
            username VARCHAR(32) NOT NULL UNIQUE,
            password VARCHAR(32) NOT NULL
        )
    ''')

    cursor.execute('''
        CREATE TABLE IF NOT EXISTS topic (
            id INTEGER,
            access_key_id INTEGER NOT NULL,
            topic_name VARCHAR(32) NOT NULL,
            FOREIGN KEY (access_key_id) REFERENCES access_auth(id)
        )
    ''')

    cursor.execute('''
        CREATE TABLE IF NOT EXISTS dialogue_history (
            id INTEGER PRIMARY KEY,
            uuid VARCHAR(36) UNIQUE NOT NULL,
            access_key_id INTEGER NOT NULL,
            created_time INTEGER NOT NULL,
            last_modified_time INTEGER NOT NULL,
            title VARCHAR(100),
            current_model VARCHAR(50),
            topic_id INTEGER,
            dialogue_content_json TEXT,
            FOREIGN KEY (access_key_id) REFERENCES access_auth(id)
        )
    ''')

    conn.commit()
    conn.close()
    